using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class TempHistoryDetailsRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class TempHistoryDetailsDbo : DatabaseObject
        {
            public TempHistoryDetailsDbo()
            {
                Name = "TempHistoryDetails";
                GeneratedClassName = "TempHistoryDetails";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Rate", ColumnType = "decimal", IsIdentity = false, IsNullable = false, Length = 9, Precision = 18, Scale = 8, GeneratedPropertyName = "Rate" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SpacesConversion", ColumnType = "decimal", IsIdentity = false, IsNullable = false, Length = 9, Precision = 18, Scale = 8, GeneratedPropertyName = "SpacesConversion" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "FuelsConversion", ColumnType = "decimal", IsIdentity = false, IsNullable = false, Length = 9, Precision = 18, Scale = 8, GeneratedPropertyName = "FuelsConversion" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CategoryId", ColumnType = "smallint", IsIdentity = false, IsNullable = true, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "CategoryId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CategoryCode", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "CategoryCode" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Category", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Category" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Universe", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Universe" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Role", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Role" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "AverageVolume", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "AverageVolume" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "AverageSales", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "AverageSales" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "AverageGIM", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "AverageGIM" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "AverageGIMP", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "AverageGIMP" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "AverageDelistedVolume", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "AverageDelistedVolume" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "AverageDelistedSales", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "AverageDelistedSales" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "AverageDelistedGIM", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "AverageDelistedGIM" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SS_VolumeCY", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "SS_VolumeCY" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SS_SalesCY", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "SS_SalesCY" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SS_GIMCY", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "SS_GIMCY" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SS_PIDiffCY", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "SS_PIDiffCY" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SS_ChargeOffCY", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "SS_ChargeOffCY" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SS_RebatesCY", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "SS_RebatesCY" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SS_InventoryCY", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "SS_InventoryCY" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SS_VolumePY", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "SS_VolumePY" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SS_SalesPY", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "SS_SalesPY" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SS_GIMPY", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "SS_GIMPY" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SS_PIDiffPY", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "SS_PIDiffPY" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SS_ChargeOffPY", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "SS_ChargeOffPY" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SS_RebatesPY", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "SS_RebatesPY" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SS_InventoryPY", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "SS_InventoryPY" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Gondola", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Gondola" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "TransactionArea", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "TransactionArea" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "GrabAndGo", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "GrabAndGo" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "WIC", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "WIC" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "OpenCooler", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "OpenCooler" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Freezer", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Freezer" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Forecourt", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Forecourt" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "OtherExhibition", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "OtherExhibition" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SKUsWithSales", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "SKUsWithSales" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "NewSKUs", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "NewSKUs" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SKUsInSalesFiles", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "SKUsInSalesFiles" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "DelistProposal", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "DelistProposal" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Sales10", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Sales10" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Sales20", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Sales20" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Sales30", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Sales30" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Sales40", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Sales40" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Sales50", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Sales50" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Sales60", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Sales60" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Sales70", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Sales70" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Sales80", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Sales80" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Sales90", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Sales90" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Sales100", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Sales100" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Bottom5", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Bottom5" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "HurdleRate", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "HurdleRate" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ActiveCategories", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "ActiveCategories" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CategoriesWithInvAnalysis", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "CategoriesWithInvAnalysis" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Country", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "Country" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Range", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Range" });
              
            }
        }
		
		public static List<TempHistoryDetailRow> Select(Expression<Func<TempHistoryDetailRow, bool>> expression)
        {
            List<TempHistoryDetailRow> rows = new List<TempHistoryDetailRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<TempHistoryDetailRow>(new TempHistoryDetailsDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<TempHistoryDetailRow> Select()
		{
			List<TempHistoryDetailRow> rows = new List<TempHistoryDetailRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<TempHistoryDetailRow>(new TempHistoryDetailsDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<TempHistoryDetailRow> PagedSelect(Expression<Func<TempHistoryDetailRow, bool>> filterExpression, Expression<Func<TempHistoryDetailRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<TempHistoryDetailRow> rows = new List<TempHistoryDetailRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<TempHistoryDetailRow>(new TempHistoryDetailsDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<TempHistoryDetailRow> TempHistoryDetailList)
        {
            TempHistoryDetailRow row;

            while (reader.Read())
            {
                row = new TempHistoryDetailRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					Rate = Util.CastDbValue<decimal>(reader["Rate"]),
					SpacesConversion = Util.CastDbValue<decimal>(reader["SpacesConversion"]),
					FuelsConversion = Util.CastDbValue<decimal>(reader["FuelsConversion"]),
					CategoryId = Util.CastDbValue<int?>(reader["CategoryId"]),
					CategoryCode = Util.CastDbValue<string>(reader["CategoryCode"]),
					Category = Util.CastDbValue<string>(reader["Category"]),
					Universe = Util.CastDbValue<string>(reader["Universe"]),
					Role = Util.CastDbValue<string>(reader["Role"]),
					AverageVolume = Util.CastDbValue<int?>(reader["AverageVolume"]),
					AverageSales = Util.CastDbValue<decimal?>(reader["AverageSales"]),
					AverageGIM = Util.CastDbValue<decimal?>(reader["AverageGIM"]),
					AverageGIMP = Util.CastDbValue<decimal?>(reader["AverageGIMP"]),
					AverageDelistedVolume = Util.CastDbValue<int?>(reader["AverageDelistedVolume"]),
					AverageDelistedSales = Util.CastDbValue<decimal?>(reader["AverageDelistedSales"]),
					AverageDelistedGIM = Util.CastDbValue<decimal?>(reader["AverageDelistedGIM"]),
					SS_VolumeCY = Util.CastDbValue<int?>(reader["SS_VolumeCY"]),
					SS_SalesCY = Util.CastDbValue<decimal?>(reader["SS_SalesCY"]),
					SS_GIMCY = Util.CastDbValue<decimal?>(reader["SS_GIMCY"]),
					SS_PIDiffCY = Util.CastDbValue<decimal?>(reader["SS_PIDiffCY"]),
					SS_ChargeOffCY = Util.CastDbValue<decimal?>(reader["SS_ChargeOffCY"]),
					SS_RebatesCY = Util.CastDbValue<decimal?>(reader["SS_RebatesCY"]),
					SS_InventoryCY = Util.CastDbValue<decimal?>(reader["SS_InventoryCY"]),
					SS_VolumePY = Util.CastDbValue<int?>(reader["SS_VolumePY"]),
					SS_SalesPY = Util.CastDbValue<decimal?>(reader["SS_SalesPY"]),
					SS_GIMPY = Util.CastDbValue<decimal?>(reader["SS_GIMPY"]),
					SS_PIDiffPY = Util.CastDbValue<decimal?>(reader["SS_PIDiffPY"]),
					SS_ChargeOffPY = Util.CastDbValue<decimal?>(reader["SS_ChargeOffPY"]),
					SS_RebatesPY = Util.CastDbValue<decimal?>(reader["SS_RebatesPY"]),
					SS_InventoryPY = Util.CastDbValue<decimal?>(reader["SS_InventoryPY"]),
					Gondola = Util.CastDbValue<decimal?>(reader["Gondola"]),
					TransactionArea = Util.CastDbValue<decimal?>(reader["TransactionArea"]),
					GrabAndGo = Util.CastDbValue<decimal?>(reader["GrabAndGo"]),
					WIC = Util.CastDbValue<decimal?>(reader["WIC"]),
					OpenCooler = Util.CastDbValue<decimal?>(reader["OpenCooler"]),
					Freezer = Util.CastDbValue<decimal?>(reader["Freezer"]),
					Forecourt = Util.CastDbValue<decimal?>(reader["Forecourt"]),
					OtherExhibition = Util.CastDbValue<decimal?>(reader["OtherExhibition"]),
					SKUsWithSales = Util.CastDbValue<int?>(reader["SKUsWithSales"]),
					NewSKUs = Util.CastDbValue<int?>(reader["NewSKUs"]),
					SKUsInSalesFiles = Util.CastDbValue<int?>(reader["SKUsInSalesFiles"]),
					DelistProposal = Util.CastDbValue<int?>(reader["DelistProposal"]),
					Sales10 = Util.CastDbValue<int?>(reader["Sales10"]),
					Sales20 = Util.CastDbValue<int?>(reader["Sales20"]),
					Sales30 = Util.CastDbValue<int?>(reader["Sales30"]),
					Sales40 = Util.CastDbValue<int?>(reader["Sales40"]),
					Sales50 = Util.CastDbValue<int?>(reader["Sales50"]),
					Sales60 = Util.CastDbValue<int?>(reader["Sales60"]),
					Sales70 = Util.CastDbValue<int?>(reader["Sales70"]),
					Sales80 = Util.CastDbValue<int?>(reader["Sales80"]),
					Sales90 = Util.CastDbValue<int?>(reader["Sales90"]),
					Sales100 = Util.CastDbValue<int?>(reader["Sales100"]),
					Bottom5 = Util.CastDbValue<int?>(reader["Bottom5"]),
					HurdleRate = Util.CastDbValue<int?>(reader["HurdleRate"]),
					ActiveCategories = Util.CastDbValue<int?>(reader["ActiveCategories"]),
					CategoriesWithInvAnalysis = Util.CastDbValue<int?>(reader["CategoriesWithInvAnalysis"]),
					Country = Util.CastDbValue<string>(reader["Country"]),
					Range = Util.CastDbValue<string>(reader["Range"])
                };
                                
                TempHistoryDetailList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(TempHistoryDetailRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<TempHistoryDetailRow>(new TempHistoryDetailsDbo(), row);
            }
        }

		public static void Update(TempHistoryDetailRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<TempHistoryDetailRow>(new TempHistoryDetailsDbo(), row);
            }
        }

		public static void Delete(Expression<Func<TempHistoryDetailRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<TempHistoryDetailRow>(new TempHistoryDetailsDbo(), expression);
            }
        }

	}
}
	
